package com.ssx.controller;

import com.alibaba.fastjson.JSON;
import com.ssx.entity.*;
import com.ssx.service.ICustomerService;
import com.ssx.service.ICustomershareService;
import com.ssx.service.ICustomervisitService;
import com.ssx.service.IEmployeesService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Package com.ssx.controller
 * @ClassName ManagerController
 * @Copyright: Copyright (c) 2020
 * @Date 2020/10/26 8:30
 * @Company www.sudaoxi.com.cn
 * @Author 苏道羲
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/managers")
public class ManagerController {
    @Autowired
    private IEmployeesService employeesService;
    @Autowired
    private ICustomervisitService customervisitService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomershareService customershareService;

    @RequestMapping("/empList")
    public String empList(HttpServletRequest request){
        //数据库第几条
        int offset= Integer.parseInt(request.getParameter("offset"));
        //每页大小
        int limit= Integer.parseInt(request.getParameter("limit"));
        //当前页
        int current=(offset/limit)+1;
        Pager<Employees> pager = null;
        //查询条件
        String search = request.getParameter("search");
        //根据条件分页查询
        List<Employees> employees = employeesService.selelctPageByConditionJsutEmp(offset, limit, search);
        pager = new Pager<>(employeesService.selelctPageCountJsutEmp(search), employeesService.selelctPageByConditionJsutEmp(offset, limit, search));
        //返回json字符串
        return JSON.toJSONString(pager);
    }

    @RequestMapping("/visitList")
    public String visitList(HttpServletRequest request){
        //数据库第几条
        int offset= Integer.parseInt(request.getParameter("offset"));
        //每页大小
        int limit= Integer.parseInt(request.getParameter("limit"));
        //当前页
        int current=(offset/limit)+1;
        Pager<Customervisit> pager = null;
        //查询条件
        String search = request.getParameter("search");
        //根据条件分页查询
        pager = new Pager<>(customervisitService.countFromManger(search), customervisitService.listFromManger(offset, limit, search));
        //返回json字符串
        return JSON.toJSONString(pager);
    }

    @RequestMapping("/delOneEmp")
    public String delOneEmp(@RequestParam(value = "id") int id){
        int delOneEmp = employeesService.delOneEmp(id);
        return delOneEmp>0?"success":"failed";
    }

    @RequestMapping("/cusList")
    public String cusList(HttpServletRequest request){
        //数据库第几条
        int offset= Integer.parseInt(request.getParameter("offset"));
        //每页大小
        int limit= Integer.parseInt(request.getParameter("limit"));
        //当前页
        int current=(offset/limit)+1;
        Pager<Customer> pager = null;
        //查询条件
        String search = request.getParameter("search");
        //根据条件分页查询
        pager = new Pager<>(customerService.countFromManger(search), customerService.listFromManger(offset, limit, search));
        //返回json字符串
        return JSON.toJSONString(pager);
    }

    /**
     * 显示一条用户记录
     *
     * @param id
     * @return
     */
    @RequestMapping("/showOneEmp")
    public String showOneEmp(int id) {
        Employees employees = employeesService.getById(id);
        return JSON.toJSONString(employees);
    }



    @RequestMapping("/editEmp")
    public String editEmp(HttpServletRequest request){
        int modify = Integer.parseInt(request.getParameter("modify"));
        Employees employees = new Employees();
        //获取用户名
        String username=request.getParameter("username");
        String hashAlgorithmName="MD5";
        //获取前端密码
        Object source=request.getParameter("password");
        Object salt= ByteSource.Util.bytes(username);
        int hashIterations=1024;
        SimpleHash simpleHash = new SimpleHash(hashAlgorithmName, source, salt, hashIterations);
        //md5加密
        String result = simpleHash.toString();
        //存进数据库
        employees.setPassword(result);
        employees.setEmpid(Integer.parseInt("".equals(request.getParameter("empid")) ?"0":request.getParameter("empid")));
        employees.setEmail(request.getParameter("email"));
        employees.setName(request.getParameter("name"));
        employees.setRole(new Role(Integer.parseInt(request.getParameter("roleId")),"",""));
        employees.setTel(request.getParameter("tel"));
        employees.setUsername(request.getParameter("username"));
        System.out.println("editEmp: "+employees);
        boolean flg;
        //0新增  1修改
        if (modify==0){
            flg = employeesService.save(employees);
        }else {
            flg=employeesService.updateById(employees);
        }
        return flg?"success":"failed";
    }
}
